Imports System.Data
Imports System.Data.SqlClient

Public Class FrmContacten

    Private Sub FrmContacten_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DocRegData2DataSet.Contacten' table. You can move, or remove it, as needed.
        Me.ContactenTableAdapter.Fill(Me.DocRegData2DataSet.Contacten)
        'TODO: This line of code loads data into the 'DocRegData2DataSet.Bedrijven' table. You can move, or remove it, as needed.
        Me.BedrijvenTableAdapter.Fill(Me.DocRegData2DataSet.Bedrijven)
        'TODO: This line of code loads data into the 'DocRegData2DataSet.Contacten' table. You can move, or remove it, as needed.

    End Sub

    Private Sub BtnVerwijderen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnVerwijderen.Click

        Dim objcommand As New SqlCommand()
        Dim intContactID As Integer
        Dim strBoodschap As String

        If Not IsNothing(ContactpersoonComboBox.SelectedValue) Then
            intContactID = ContactpersoonComboBox.SelectedValue
            strBoodschap = "Weet U zeker dat u deze klant " & "wilt verwijderen?"

            With objcommand
                .Connection = New SqlConnection(My.Settings.Docregdata2ConnectionString)
                .CommandText = "DELETE FROM Contacten " & "Where ContactenID= " & intContactID
                .Connection.Open()
                .ExecuteNonQuery()
            End With

            ContactenTableAdapter.Update(DocRegData2DataSet.Contacten)
            ContactenTableAdapter.Fill(DocRegData2DataSet.Contacten)

            MsgBox("Contactpersoon is verwijderd!")

        End If

    End Sub


    Private Sub BtnHoofdmenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHoofdmenu.Click
        Me.Visible = False
        My.Forms.FrmHoofdmenu.Visible = True

    End Sub

    Private Sub BtnBedrijven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBedrijven.Click
        Me.Visible = False
        My.Forms.Bedrijven.Visible = True

    End Sub

    Private Sub BtnOpslaan_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpslaan.Click

        Dim BedrijvenID As Integer = Me.CmbNieuwcontact.SelectedValue
        Dim contactpersoon As String = Me.TxtNieuwContact.Text
        Dim mobielnummer As String = Me.TxtMobielnummer.Text
        Dim email As String = Me.TxtEmail.Text

        Dim objdatarow As DataRow
        objdatarow = DocRegData2DataSet.Tables("Contacten").NewRow

        objdatarow("Contactpersoon") = contactpersoon
        objdatarow("BedrijvenID") = BedrijvenID
        objdatarow("Mobiele Telefoon") = mobielnummer
        objdatarow("e-mail") = email

        DocRegData2DataSet.Tables("Contacten").Rows.Add(objdatarow)

        ContactenTableAdapter.Update(DocRegData2DataSet.Contacten)
        ContactenTableAdapter.Fill(DocRegData2DataSet.Contacten)

        MsgBox("Contactpersoon is toegevoegd!")

    End Sub


    Private Sub BtnWijzigen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWijzigen.Click

        Dim NaamOud As String = Me.ContactpersoonComboBox.Text
        Dim Naamnieuw As String = Me.TxtNaamNieuw.Text

        Dim cmd As New SqlCommand

        If Me.TxtNaamNieuw.Text = "" = False Then

            With cmd
                .CommandType = CommandType.Text
                .CommandText = "Update Contacten SET Contactpersoon = @Naamnieuw Where Contactpersoon = @NaamOud"
                .Connection = New SqlConnection(My.Settings.Docregdata2ConnectionString)
                .Parameters.AddWithValue("@NaamOud", NaamOud)
                .Parameters.AddWithValue("@naamNieuw", Naamnieuw)
                .Connection.Open()
                .ExecuteNonQuery()
            End With

        Else

        End If

        If Me.TxtEmailnieuw.Text = "" = False Then

            Dim emailoud As String = Me.E_mailTextBox.Text
            Dim emailnieuw As String = Me.TxtEmailnieuw.Text

            With cmd
                .CommandType = CommandType.Text
                .CommandText = "Update Contacten SET [E-mail] = @Emailnieuw Where [E-mail] = @emailoud"
                .Connection = New SqlConnection(My.Settings.Docregdata2ConnectionString)
                .Parameters.AddWithValue("Emailoud", emailoud)
                .Parameters.AddWithValue("Emailnieuw", emailnieuw)
                .Connection.Open()
                .ExecuteNonQuery()
            End With

        Else

        End If

        If Me.TxtMobielNieuw.Text = "" = False Then

            Dim mobieloud As String = Me.Mobiele_telefoonTextBox.Text
            Dim mobielnieuw As String = Me.TxtMobielNieuw.Text

            With cmd
                .CommandType = CommandType.Text
                .CommandText = "Update Contacten SET [Mobiele telefoon]=@mobielnieuw Where [Mobiele telefoon]=@mobieloud"
                .Connection = New SqlConnection(My.Settings.Docregdata2ConnectionString)
                .Parameters.AddWithValue("@Mobieloud", mobieloud)
                .Parameters.AddWithValue("@Mobielnieuw", mobielnieuw)
                .Connection.Open()
                .ExecuteNonQuery()
            End With

        Else

        End If

        ContactenTableAdapter.Update(DocRegData2DataSet.Contacten)
        ContactenTableAdapter.Fill(DocRegData2DataSet.Contacten)

        MsgBox("Contactpersoon is gewijzigd!")

    End Sub

  
End Class
